package com.longjianghuasheng.logistics.business.order.fragment

import android.os.Bundle
import androidx.fragment.app.viewModels
import com.longjianghuasheng.logistics.R
import com.longjianghuasheng.logistics.base.BaseMultiStateFragment
import com.longjianghuasheng.logistics.business.mine.viewmodel.MineViewModel
import com.longjianghuasheng.logistics.business.order.adapter.OrderAdapter
import com.longjianghuasheng.logistics.business.order.adapter.OrderAdapter.Companion.BT_ADDITIONAL_SERVICES
import com.longjianghuasheng.logistics.business.order.adapter.OrderAdapter.Companion.BT_CANCEL
import com.longjianghuasheng.logistics.business.order.adapter.OrderAdapter.Companion.BT_CONFIRM_COMPLETED
import com.longjianghuasheng.logistics.business.order.adapter.OrderAdapter.Companion.BT_CONFIRM_THE_RECEIPT_OF_GOODS
import com.longjianghuasheng.logistics.business.order.adapter.OrderAdapter.Companion.BT_DELETE
import com.longjianghuasheng.logistics.business.order.adapter.OrderAdapter.Companion.BT_PAYMENT
import com.longjianghuasheng.logistics.business.order.adapter.OrderAdapter.Companion.BT_SIGNED
import com.longjianghuasheng.logistics.business.order.adapter.OrderAdapter.Companion.BT_SIGN_UP_ENTRANCE
import com.longjianghuasheng.logistics.business.order.adapter.OrderAdapter.Companion.BT_TRAJECTORY
import com.longjianghuasheng.logistics.business.order.viewmodel.OrderViewModel
import com.longjianghuasheng.logistics.databinding.FragmentOrderBinding
import com.longjianghuasheng.logistics.eventViewModel
import com.longjianghuasheng.logistics.ext.*
import com.longjianghuasheng.logistics.network.NetworkApi.Companion.BASE_URL

class OrderFragment : BaseMultiStateFragment<OrderViewModel, FragmentOrderBinding>() {

    var type = 0

    var status = 0


    override fun isRefresh(): Boolean = true

    override fun isLoadMore(): Boolean = true

    private val orderAdapter by lazy {
        OrderAdapter()
    }

    private val mineViewModel by viewModels<MineViewModel>()
    override fun layoutId(): Int = R.layout.fragment_order

    override fun initView(savedInstanceState: Bundle?) {
        super.initView(savedInstanceState)
        orderAdapter.run {
            mDatabind.recyclerView.initLinerDecoration(adapter = this).setHasFixedSize(false)

            addChildClickViewIds(R.id.tv_phone, R.id.iv_qr_code)
            setOnItemChildClickListener { adapter, view, position ->
                when (view.id) {
                    R.id.tv_phone -> mineViewModel.hot_line()
                    R.id.iv_qr_code -> showQrCodeDialog("$BASE_URL/${data[position].ewm1}")
                    else -> {}
                }

            }


            onClickListener = { position, str ->
                mViewModel.id = data[position].id ?: 0

                when (str) {
                    BT_SIGN_UP_ENTRANCE -> navigate(R.id.signUpFragment, Bundle().apply {
                        putInt("id", mViewModel.id)
                    })
                    BT_CANCEL -> hintDialog("确定取消吗？", {
                        mViewModel.order_del()
                    })
                    BT_DELETE -> deleteDialog {

                        mViewModel.order_del()
                    }
                    BT_SIGNED -> {

                        navigate(R.id.signUpFragment, Bundle().apply {
                            putInt("id", mViewModel.id)
                        })
                    }
                    BT_PAYMENT -> {}
                    BT_TRAJECTORY -> {
                        navigate(R.id.orderDetailFragment, Bundle().apply {
                            putInt("id", mViewModel.id)

                        })
                    }
                    BT_CONFIRM_COMPLETED, BT_CONFIRM_THE_RECEIPT_OF_GOODS -> {
                        mViewModel.order_done()
                    }
                    BT_ADDITIONAL_SERVICES -> {

                        showEdtDialog("追加服务：", "请填写需要的追加服务", data[position].zjfw ?: "", false) {

                            mViewModel.con = it
                            mViewModel.order_zjfw()
                        }

                    }
                    else -> {}
                }


            }
        }


    }

    override fun createObserver() {
        super.createObserver()
        mViewModel.result.observer(this) {
            mViewModel.isLoaded = true
            if (mViewModel.page == 1) {

                if (it.data.isNullOrEmpty()) {

                    mStatefulLayout.showEmpty()
                    return@observer
                }
                orderAdapter.setNewInstance(it.data)
                return@observer
            }

            if (it.data.isNullOrEmpty()) {

                mViewModel.page--
                smartLayout.setNoMoreData(true)
                return@observer
            }


            orderAdapter.addData(it.data ?: mutableListOf())


        }
        mViewModel.deleteNotify.observer(this) {

            orderAdapter.removeAt(orderAdapter.data.indexOfFirst { it.id == mViewModel.id })


        }
        mViewModel.notify.observer(this) {

            eventViewModel.onOrderChangeEvent.value = 0
        }
        eventViewModel.orderSearchEvent.observeInFragment(this) {

            mViewModel.keywords = it

            if (isVisible) {
                smartLayout.autoRefresh()
            }

        }
        mineViewModel.resultHot_line.observer(this) {
            openPhone(it)
        }
        eventViewModel.onOrderChildSelectEvent.observeInFragment(this) {

            if (status == it && mViewModel.isLoaded) {
                mViewModel.page = 1
                lazyLoadData()
            }
        }
        eventViewModel.onOrderChangeEvent.observeInFragment(this) {
            smartLayout.autoRefresh()
        }

    }

    override fun initData() {
        super.initData()
        mViewModel.type = type
        mViewModel.status = status
    }

    override fun lazyLoadData() {
        super.lazyLoadData()
        mViewModel.order_list()
    }

    companion object {
        fun newInstance(type: Int, status: Int, outIndex: Int): OrderFragment =
            OrderFragment().also {
                it.type = type
                it.status = status
                it.orderAdapter.type = outIndex
            }
    }
}